package com.wavehi.file_up_load.web.controller;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;

/**
 * @Author WaveHi
 * @Date 13/11/2021 下午 3:56
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Controller
public class FileController {

    @RequestMapping("/upLoad")
    public String fileUpLoad(@RequestPart(value = "file", required = false) MultipartFile file,
                             @RequestPart(value = "files", required = false) MultipartFile[] files) throws IOException {
        File file1 = ResourceUtils.getFile("src/main/resources/static/file/upLoad");
        if(file != null) {
            if (!file.isEmpty()) {
                InputStream inputStream = file.getInputStream();
                String originalFilename = file.getOriginalFilename();
                String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
                String fileName = UUID.randomUUID().toString() + substring;
                FileOutputStream fileOutputStream = new FileOutputStream(new File(file1, fileName));
                IOUtils.copy(inputStream, fileOutputStream);
                inputStream.close();
                fileOutputStream.close();
            }
        }

        String originalFilename = null;
        if(files != null) {
            for (MultipartFile multipartFile : files) {
                if (!multipartFile.isEmpty()) {
                    originalFilename = multipartFile.getOriginalFilename();
                    String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
                    String fileName = UUID.randomUUID().toString() + substring;
                    InputStream inputStream = multipartFile.getInputStream();
                    FileOutputStream fileOutputStream = new FileOutputStream(new File(file1, fileName));
                    IOUtils.copy(inputStream, fileOutputStream);
                    inputStream.close();
                    fileOutputStream.close();
                }
            }
        }
        return "complete";
    }

    @RequestMapping("/singleFile")
    public String singleFile(@RequestPart("file") MultipartFile file) throws IOException {
        if(file != null) {
            if(!file.isEmpty()) {
                File destDir =
                        ResourceUtils
                                .getFile("src/main/resources/static/file/upLoad");
                String originalFilename = file.getOriginalFilename();
                if(originalFilename != null) {
                    String substring =
                            originalFilename.substring(originalFilename.lastIndexOf("."));
                    String fileName = UUID.randomUUID().toString() + substring;
                    InputStream inputStream = file.getInputStream();
                    FileOutputStream fileOutputStream = new FileOutputStream(new File(destDir, fileName));
                    IOUtils.copy(inputStream, fileOutputStream);
                    inputStream.close();
                    fileOutputStream.close();
                }
            }
        }
        return "complete";
    }

    @RequestMapping("/multiFiles")
    public String multiFiles(@RequestPart("files") MultipartFile[] files) throws IOException {
        File destDir =
                ResourceUtils
                        .getFile("src/main/resources/static/file/upLoad");
        if(files != null) {
            for (MultipartFile file : files) {
                if(file != null) {
                    if (!file.isEmpty()) {
                        String originalFilename = file.getOriginalFilename();
                        String substring =
                                originalFilename.substring(originalFilename.lastIndexOf("."));
                        String fileName = UUID.randomUUID().toString() + substring;
                        InputStream inputStream = file.getInputStream();
                        FileOutputStream fileOutputStream =
                                new FileOutputStream(new File(destDir, fileName));
                        IOUtils.copy(inputStream, fileOutputStream);
                        inputStream.close();
                        fileOutputStream.close();
                    }
                }
            }
        }
        return "complete";
    }
}
